import obj from '../../api/good.js'
import category from '../../api/category.js'

let good = {
  created() {
    this.findPage()
  },
  data() {
    return {
      props:{
        value:"id",
        label:"name",
        emitPath:false
      },
      tableData: [],
      formData: {},
      currentPage: 1,
      pageSize: 3,
      total: 0,
      options:[],
      dialogVisible: false,
      dialogVisible1: false
    }
  },
  methods: {
    async findPage() {
      let result = await obj.findAllByPage(this.currentPage, this.pageSize)
      this.tableData = result.record;
      this.total = result.total;
    },
    /**
     * 点击分页按钮换页
     */
    pageChangeHandle(page) {
      this.currentPage = page;
      this.findPage()
    },
    /**
     * 展示新建弹框
     */
    showDialog() {
      this.formData = {};
      this.getSelectData();
      this.dialogVisible = true;
    },
    /**
     * 增加或修改操作
     */
    async addOrUpdate() {
      if (this.formData.goodCode) {
        await obj.update(this.formData);
      } else {
        await obj.add(this.formData);
      }
      this.findPage();
      this.formData = {};
    },
    /**
     * 点击编辑按钮，将数据展示在表单中
     */
    async findById(id) {
      this.getSelectData();
      this.formData = await obj.findById(id);
    },
    /**
     * 级联选择器内数据获取
     */
    async getSelectData(){
      this.options = await category.findAll();
    },

    /**
     * 删除
     */
    async del() {
      await obj.delete(this.formData.id);
      this.findPage();
    }


  }

};


export default good;
